package cc.wanforme.st.server.base.controller;


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cc.wanforme.st.server.base.entity.Permission;
import cc.wanforme.st.server.base.service.IPermissionService;
import cc.wanforme.st.server.base.service.IRolePermissionService;
import cc.wanforme.st.server.base.service.cache.PermissionCacheService;
import cc.wanforme.st.server.util.AssertCheck;
import cc.wanforme.st.server.vo.ResMessage;
import cc.wanforme.st.server.vo.SearchQueryVo;
import cc.wanforme.st.server.vo.base.PermissionTreeVo;
import cc.wanforme.st.server.vo.base.PermissionVo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanne
 * @since 2022-03-02
 */
@Controller
@RequestMapping("/base/permission")
public class PermissionController {

	@Autowired
	private IPermissionService permissionService;
	@Autowired
	private IRolePermissionService rolePermissionService;
	@Autowired
	private PermissionCacheService permissionCacheService;
	
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('permission:add')")
	@ResponseBody
	public ResMessage<Permission> add(@RequestBody PermissionVo permission) {
		Permission result = permissionService.saveVo(permission);
		return ResMessage.success(result);
	}
	
	@PostMapping("/update")
	@PreAuthorize("hasAuthority('permission:update')")
	@ResponseBody
	public ResMessage<Void> update(@RequestBody PermissionVo permission) {
		permissionService.updateVo(permission);
		return ResMessage.success();
	}

	@PostMapping("/del/{id}")
	@PreAuthorize("hasAuthority('permission:delete')")
	@ResponseBody
	@Transactional
	public ResMessage<Void> del(@PathVariable String id ) {
		Permission permission = permissionService.getById(id);
		AssertCheck.nonNull(permission, "权限不存在");
		
		// 删除 role 中的权限
		rolePermissionService.deleteAllPermission(permission.getCode());
		
		// 删除权限
		permissionService.deleteVo(id);
		
		return ResMessage.success();
	}
	
	/** 查询，可以根据 code 和 description 模糊查询
	 * @param queryVo
	 * @return
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('permission:list')")
	@ResponseBody
	public ResMessage<PageInfo<PermissionVo>> list(@RequestBody SearchQueryVo<Permission> queryVo) {
		Permission param = queryVo.getParam();
		PageInfo<PermissionVo> result;
		if(param != null) {
			result = permissionService.searchPermissions(queryVo.getPage(), queryVo.getSize(),
					param.getCode(), param.getDescription());
		} else {
			result = permissionService.searchPermissions(queryVo.getPage(), queryVo.getSize(),
					null, null);
		}
		return ResMessage.success(result);
	}
	
	/** 所有权限，树形结构
	 * @param queryVo
	 * @return
	 */
	@PostMapping("/tree")
	//@PreAuthorize("hasAuthority('permission:tree')")
	@PreAuthorize("hasAuthority('permission:list')")
	@ResponseBody
	public ResMessage<List<PermissionTreeVo>> permissionTree() {
		List<PermissionTreeVo> result = permissionService.permissionTree();
		return ResMessage.success(result);
	}
	/** 清除树形结构缓存
	 * @param queryVo
	 * @return
	 */
	@PostMapping("/clearTree")
	//@PreAuthorize("hasAuthority('permission:tree')")
	@PreAuthorize("hasAuthority('permission:update')")
	@ResponseBody
	public ResMessage<Void> clearTree() {
		permissionCacheService.clearTree();
		return ResMessage.success();
	}
	
}
